/*
 * Copyright (c) 2011, 2012 Alexander Kluth <alexander.kluth@outlook.com>
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package hardyhttp.config;


import hardyhttp.filesystem.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

/**
 * Class for reading the configuration file config.yml and
 * configuring everything based on this file.
 * 
 * @author Alexander Kluth
 */
public class ServerConfig 
{
    Logger logger;
    YAMLParser yaml;
    Filesystem fs = new Filesystem();
    String configfile = fs.getCwd() + File.separator + "config.yml";
    
    Map<String, Object> configuration;
    
    /// Configuration variables, read from config.xml
    public int port;
    public int loglevel;
    public String htdocs;
    public String php;
    public boolean daemon;
    
    /**
     * C'tor, sets up the YML parser
     * 
     * @param logger 
     */
    public ServerConfig(Logger logger)
    {
        this.logger = logger;
        yaml = new YAMLParser();
    }
    
    
    /**
     * Read the configuration file and configure everything.
     * 
     * @return true if configuration was successfull, false otherwise
     */
    public boolean readConfigurationFile()
    {
        try
        {
            yaml.loadFile(configfile);
            configuration = yaml.getYamlList();
        }
        
        catch (FileNotFoundException fnfe) 
        { 
            logger.logEvent("[CONFIG] Error while reading configuration file: " + fnfe.getLocalizedMessage());
            return false;
        }
        
        //TODO: Write a generic wrapper-function which accepts the requested
        //      type
        this.port = (Integer) configuration.get("port");
        this.htdocs = (String) configuration.get("htdocs");
        this.php = (String) configuration.get("php");
        this.daemon = (Boolean) configuration.get("daemon");
        this.loglevel = (Integer) configuration.get("log");
        
        return true;
    }
    
    
    /**
     * Start the configuring process
     * 
     * @return 
     */
    public boolean configure()
    {
        if (!readConfigurationFile())
        {
            logger.logEvent("[CONFIG] Using default configuration.");
            useDefaultConfig();
            setConfig();
            return false;
        }
        
        setConfig();
        
        return true;
    }
    
    
    /**
     * If some configuration need calls to other methods, do them here.
     */
    public void setConfig()
    {
        if (!logger.setLogLevel(loglevel))
            logger.logEvent("[CONFIG] Error: Could not change log-level!");
    }
    
    
    /**
     * If reading the config-file failed, we use a default configuration
     */
    public void useDefaultConfig()
    {
        port = 80;      // Default webserver-port, might need root-privileges
        loglevel = 2;   // Log everything to a logfile
        htdocs = fs.getCwd() + File.separator + "htdocs";
        php = null;     
        daemon = false;
    }
}
